//
//  PictorialDetailView.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "PictorialDetailView.h"
#import "PictorialNavBar.h"
#import "ShareView.h"
#import "ShareEditView.h"
#import "HomePictureScrollView.h"
#import "WallpaperDownloadView.h"

#import <Masonry.h>
#import <UIImageView+WebCache.h>

#define INTERVAL_LITTLE 10
#define INTERVAL_MIDDLE 15
#define INTERVAL_MAX    20

@interface PictorialDetailView () <UIScrollViewDelegate,PictorialNavBarDelegate,ShareViewDelegate,ShareEditViewDelegate>

@property (nonatomic,strong) PictorialNavBar *navbar;
@property (nonatomic,strong) UIScrollView *scrollView;

@end
@implementation PictorialDetailView

-(void)willMoveToSuperview:(UIView *)newSuperview{
    self.frame = newSuperview.bounds;
    self.backgroundColor = [UIColor clearColor];
    
    [self createUI];
}
-(void)setModel:(PictorialArticleModel *)model{
    _model = model;
        
    UIImageView *picture = [UIImageView classInitializtion];
    [self.scrollView addSubview:picture];
    [picture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.and.trailing.equalTo(self.scrollView).offset(0);
        make.height.mas_equalTo(WIDTH * 1.2);
        make.centerX.equalTo(self.scrollView);
    }];
    
    UIImageView *littlePicture = [UIImageView classInitializtion];
    [self.scrollView addSubview:littlePicture];
    littlePicture.image = [UIImage imageNamed:@"foot.png"];
    [littlePicture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(picture.mas_bottom).offset(INTERVAL_MAX);
        make.leading.equalTo(self.scrollView).offset(INTERVAL_MIDDLE);
        make.width.height.mas_equalTo(30);
    }];
    
    UILabel *titleLabel = [UILabel classInitializtion];
    [self.scrollView addSubview:titleLabel];
    titleLabel.numberOfLines = 0;
    titleLabel.font = Font(18);
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(littlePicture.mas_trailing).offset(INTERVAL_MAX);
        make.width.mas_equalTo(WIDTH - INTERVAL_MAX * 2);
        make.bottom.equalTo(littlePicture.mas_bottom);
    }];
    
    UIView *line = [UIView classInitializtion];
    [self.scrollView addSubview:line];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.leading.equalTo(self.scrollView).offset(INTERVAL_MIDDLE);
        make.width.mas_equalTo(WIDTH - INTERVAL_MIDDLE * 2);
        make.top.equalTo(titleLabel.mas_bottom).offset(INTERVAL_MAX);
    }];
    
    UILabel *descLabel = [UILabel classInitializtion];
    [self.scrollView addSubview:descLabel];
    descLabel.numberOfLines = 0;
    descLabel.font = Font(15);
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.scrollView).offset(INTERVAL_LITTLE);
        make.width.mas_equalTo(WIDTH - INTERVAL_LITTLE * 2);
        make.top.equalTo(line.mas_bottom).offset(INTERVAL_LITTLE);
    }];
    
    [picture imageLoadProgressWithNetImage:model.image_url];
    titleLabel.text = model.title;
    descLabel.text = model.desc;

    
    UIImageView *lastView = nil;
    
    for (int index = 0; index < model.description_notes.count; index ++) {
        NSDictionary *dict = model.description_notes[index];
        NSString *content = dict[@"description"];
        NSString *pictureString = dict[@"image_url"];
        
        UILabel *contentLabel = [UILabel classInitializtion];
        [self.scrollView addSubview:contentLabel];
        contentLabel.numberOfLines = 0;
        contentLabel.font = Font(15);
        
        UIImageView *imageView = [UIImageView classInitializtion];
        [self.scrollView addSubview:imageView];
        
        imageView.userInteractionEnabled = YES;
        imageView.tag = index + 500;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(show:)];
        [imageView addGestureRecognizer:tap];
        
        if (index == 0) {
            [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self.scrollView).offset(INTERVAL_LITTLE);
                make.width.mas_equalTo(WIDTH - INTERVAL_LITTLE * 2);
                make.top.equalTo(descLabel.mas_bottom).offset(INTERVAL_MAX);
            }];
        }else{
            [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self.scrollView).offset(INTERVAL_LITTLE);
                make.width.mas_equalTo(WIDTH - INTERVAL_LITTLE * 2);
                make.top.equalTo(lastView.mas_bottom).offset(INTERVAL_MAX);
            }];
        }
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(contentLabel.mas_bottom).offset(INTERVAL_LITTLE);
            make.width.mas_equalTo(WIDTH - INTERVAL_LITTLE * 2);
            make.height.mas_equalTo(250);
            make.centerX.equalTo(self.scrollView);
        }];
        
        contentLabel.text = content;
        [imageView imageLoadProgressWithNetImage:pictureString];
        
        lastView = imageView;
    }
    
    UIView *lastDescView = nil;
    if (model.description_notes.count <= 0) {
        lastDescView = descLabel;
    }else{
        lastDescView = lastView;
    }
    [lastDescView layoutIfNeeded];
    [lastDescView setNeedsLayout];
    
    UIView *lastPhotoView = nil;
    
    NSArray *pictureArray = model.photos;
    
    CGFloat interval = INTERVAL_LITTLE;
    CGFloat x = interval;
    CGFloat y = CGRectGetMaxY(lastDescView.frame) + interval;
    CGFloat w = (WIDTH - interval * 3) * 0.5;
    CGFloat h = w;
    CGFloat maxHeight = 0;
    
    for(int index = 0; index < pictureArray.count; index ++){
        
        NSDictionary *dict = pictureArray[index];
        
        UIImageView *imageView = [UIImageView imageViewWithNetImage:dict[@"image_url"] frame:CGRectMake(x, y, w, h)];
        [self.scrollView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        imageView.userInteractionEnabled = YES;
        imageView.tag = index + 100;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downLoad:)];
        [imageView addGestureRecognizer:tap];
        
        index % 2 == 1 ? (x = interval, y += interval + h):(x += interval + w);
        
        if (index == (pictureArray.count - 1)) {
            maxHeight = CGRectGetMaxY(imageView.frame) + 20;
            lastPhotoView = imageView;
        }
    }
    
    UILabel *pictureLabel = [UILabel classInitializtion];
    [self.scrollView addSubview:pictureLabel];
    pictureLabel.textColor = [UIColor grayColor];
    pictureLabel.font = Font(14);
    [pictureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.scrollView).offset(INTERVAL_MIDDLE);
        
        if (lastPhotoView == nil) {
            make.top.equalTo(lastDescView.mas_bottom).offset(INTERVAL_MIDDLE);
        }else{
            make.top.equalTo(lastPhotoView.mas_bottom).offset(INTERVAL_MIDDLE);
        }
    }];
    
    UILabel *textLabel = [UILabel classInitializtion];
    [self.scrollView addSubview:textLabel];
    textLabel.textColor = [UIColor grayColor];
    textLabel.font = Font(14);
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.scrollView).offset(INTERVAL_MIDDLE);
        make.top.equalTo(pictureLabel.mas_bottom).offset(INTERVAL_MIDDLE);
        make.bottom.equalTo(self.scrollView).offset(-INTERVAL_MAX);
    }];
    
    pictureLabel.text = [NSString stringWithFormat:@"图:%@", model.photo_author];

    textLabel.text = [NSString stringWithFormat:@"图:%@", model.text_author];
    
}
-(void)createUI{
    self.scrollView = [UIScrollView classInitializtion];
    [self addSubview:self.scrollView];
    self.scrollView.bounces = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
    self.scrollView.frame = self.bounds;
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.navbar = [PictorialNavBar nibInitializtion];
    [self addSubview:self.navbar];
    self.navbar.delegate = self;
    
}
-(void)show:(UITapGestureRecognizer *)tap {
    NSArray *pictureArray = self.model.description_notes;
    
    NSDictionary *dict = pictureArray[tap.view.tag - 500];
    
    WallpaperDownloadView *downloadView = [WallpaperDownloadView nibInitializtion];
    [self addSubview:downloadView];
    downloadView.title = self.model.title;
    downloadView.pictureString = dict[@"image_url"];
    downloadView.desc = dict[@"description"];
}
-(void)downLoad:(UITapGestureRecognizer *)tap{
    
    NSArray *pictureArray = self.model.photos;
    
    HomePictureScrollView *pictureScrollView = [HomePictureScrollView classInitializtion];
    [self addSubview:pictureScrollView];
    pictureScrollView.title = self.model.title;
    pictureScrollView.desc = self.model.attraction[@"name"];
    pictureScrollView.pictures = pictureArray;
    pictureScrollView.index = tap.view.tag - 100;
}
-(void)share{
    
    ShareView *shareView = [[[NSBundle mainBundle] loadNibNamed:@"SharePageView" owner:nil options:nil] firstObject];
    [self addSubview:shareView];
    shareView.delegate = self;

}
#pragma mark - ShareEditViewDelegate
-(void)shareOnPlatform:(NSInteger)type content:(NSString *)content{
    [_delegate shareWithContent:content picture:self.model.image_url platform:type];
}
#pragma mark - ShareViewDelegate
-(void)shareWithType:(NSInteger)type{
    ShareEditView *shareEditView = [ShareEditView nibInitializtion];
    [self addSubview:shareEditView];
    
    shareEditView.content = [NSString stringWithFormat:@"来足迹画报，领略各地风景----\n%@",self.model.title];
    shareEditView.picture = self.model.image_url;
    shareEditView.type = type;
    shareEditView.delegate = self;
}
-(void)back{
    [_delegate back];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat value = (scrollView.contentOffset.y / 300.0);
    self.navbar.bgView.alpha = value;
}

@end
